package com.itheima.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.ssm.domain.Orders;
import com.itheima.ssm.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(path = "/orders")
public class OrdersController {
    @Autowired
    public IOrdersService iOrdersService;

    /**
     * 未分页
     */
//    @RequestMapping(path = "/findall")
//    public ModelAndView findAll() throws Exception {
//        ModelAndView mv = new ModelAndView();
//        List<Orders> ordersList = iOrdersService.findAll();
//        mv.addObject("ordersList", ordersList);
//        mv.setViewName("orders-list");
//        return mv;
//    }
//    分页
    @Secured("ROLE_ADMIN")
    @RequestMapping(path = "/findall")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") Integer size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Orders> ordersList = iOrdersService.findAll(page, size);
        //pageinfo就是一个分页bean
        PageInfo pageInfo = new PageInfo(ordersList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }

    //    订单详情
    @RequestMapping(path = "/findById")
    public ModelAndView findById(@RequestParam(name = "id", required = true) String orderId) throws Exception {
        ModelAndView mv = new ModelAndView();
        Orders orders = iOrdersService.findById(orderId);
        mv.addObject("orders", orders);
        mv.setViewName("orders-show");
        return mv;
    }

}
